/**
 * Created by LeungZ on 2016/9/9.
 */
import fileTmp from './form/file.html';
import fileCtrl from './form/file.ctrl.js';

class Detail {
    constructor(detail, verList, imageSrv, ctgNotification, $stateParams, $window, $uiModal) {
        "ngInject";
        this._imageSrv = imageSrv;
        this._notification = ctgNotification;
        this.imageId = $stateParams.imageId;
        this._window = $window;
        this._uiModal = $uiModal;

        if (detail) {
            this.imagePath = detail.imagepath;
            this.name = detail.imageName;
            this.type = detail.ntype;
            this.typeName = detail.ntypeName;
            this.createTime = detail.createTime;
            this.updateTime = detail.lastUpdateTime;
            this.download = detail.downloadAddr;
            this.ver = detail.version;
            this.memo = detail.titeDesc;
        }
        this.verPage = {
            currentPage: 1,
            totalItems: 0
        };
        if (verList) {
            this.verTable = verList.list;
            this.verPage.totalItems = verList.total
        } else this.verTable = [];
    }

    getList() {
        this.verTable = null;
        this._imageSrv.getImageVers({id: this.imageId, currentPage: 1}).then(data=> {
            this.verTable = data.list;
            this.verPage.totalItems = data.total
        }).catch(data=> {
            this.verTable = [];
            this.verPage.totalItems = 0;
            this._notification.error('获取Docker镜像版本列表数据有误.');
        });
    }

    copyToClipBoard() {
        if (this._window.getSelection && this._window.document.createRange) {
            let range = this._window.document.createRange(), sel = this._window.getSelection();
            sel.removeAllRanges();
            range.selectNode(angular.element('#copyText')[0]);
            sel.addRange(range);
            this._window.document.execCommand('copy');
        }
    }

    openFileForm(elem) {
        this._uiModal.open({
            animation: true,
            templateUrl: fileTmp,
            controller: fileCtrl,
            controllerAs: 'vm',
            size: 'lg',
            resolve: {
                dockerFile: function () {
                    return elem.dockerfile;
                }
            }
        });
    }
}
export default Detail